package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.query.ServiceItemQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.impl.BpmnInfoServiceImpl;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 岗位控制器
 * @author RNIZ
 */
@Controller
@RequestMapping("business/serviceItem")
public class ServiceItemController {
    /**模板前缀*/
    private static final String PREFIX = "business/serviceItem/";

    @Autowired
    private IServiceItemService serviceItemService;

    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private IUserService userService;
    /**页面
    列表*/
    @RequiresPermissions("business:serviceItem:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return PREFIX + "list";
    }

    /**
     * 添加页面
     *
     * @return {@link String}
     */
    @RequiresPermissions("business:serviceItem:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return PREFIX + "add";
    }


    /**
     * 编辑页面
     *
     * @param id    id
     * @param model 模型
     * @return {@link String}
     */
    @RequiresPermissions("business:serviceItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("serviceItem", serviceItemService.get(id));
        return PREFIX + "edit";
    }

    /**数据
     列表*/
    @RequiresPermissions("business:serviceItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ServiceItem> query(ServiceItemQueryObject qo){
        return serviceItemService.query(qo);
    }

    /**
     * 新增
     * @param serviceItem
     * @return
     */
    @RequiresPermissions("business:serviceItem:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ServiceItem serviceItem){
        serviceItemService.save(serviceItem);
        return AjaxResult.success();
    }

    /**
     * 编辑
     *
     * @param serviceItem 服务项目
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:serviceItem:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ServiceItem serviceItem){
        serviceItemService.update(serviceItem);
        return AjaxResult.success();
    }

    /**
     * 上架
     *
     * @param id id
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:serviceItem:saleOn")
    @RequestMapping("/saleOn")
    @ResponseBody
    public AjaxResult saleOne(Long id){
        serviceItemService.saleOne(id);
        return AjaxResult.success();
    }

    /**
     * 下架
     *
     * @param id id
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:serviceItem:saleOff")
    @RequestMapping("/saleOff")
    @ResponseBody
    public AjaxResult saleOff(Long id){
        serviceItemService.saleOff(id);
        return AjaxResult.success();
    }
    /**数据
     列表*/
    @RequiresPermissions("business:serviceItem:list")
    @RequestMapping("/selectAllSaleOnList")
    @ResponseBody
    public TablePageInfo<ServiceItem> selectAllSaleOnList(ServiceItemQueryObject qo){
        qo.setSaleStatus(ServiceItem.SALESTATUS_ON);
        return serviceItemService.query(qo);
    }

    @RequiresPermissions("business:serviceItem:auditPage")
    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        model.addAttribute("serviceItem", serviceItemService.get(id));
        model.addAttribute("bpmnInfo",bpmnInfoService.getByType("car_package"));
        List<User> shopOwner = userService.queryByRoleKey("shopOwner");
        model.addAttribute("directors",shopOwner);
        List<User> 	financial = userService.queryByRoleKey("financial");
        model.addAttribute("finances",financial);
        return PREFIX + "auditPage";
    }

    @RequiresPermissions("business:serviceItem:startAudit")
    @RequestMapping("/startAudit")
    @ResponseBody
    public AjaxResult startAudit(Long id,Long bpmnInfoId,Long director,Long finance,String info){
        serviceItemService.startAudit(id,bpmnInfoId,director,finance,info);
        return AjaxResult.success();
    }
}
